//
//  UINavigationController-Addition.m
//  MClient
//
//  Created by  史正烨 on 10-9-7.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCNavigationController.h"
#import "MCTabBarController.h"
#import "MenuController.h"
#import "LoginCheckController.h"
#import "QueryController.h"
#import "MCPopupViewController.h"
#import "SelectFieldViewController.h"
#import "LookupController.h"
#import "TreeLookupController.h"
#import "AListLookupController.h"
#import "FileListViewController.h"
#import "CustomAListLookupController.h"
#import "CustomLookupController.h"
#import "TextFieldViewController.h"
#import "AListController.h"
#import "MCTabBarNavigationController.h"

@implementation UINavigationController(MClientNavi)
- (UIImage*)bgImage { return nil;}
- (void)setBgImage:(UIImage*)bgImage {}
@end

@implementation MCNavigationController
@synthesize identifierId;

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.navigationBar.tintColor = [MCUserDefaults instance].skin_navigationBarColor;
	
	skin_index = [MCUserDefaults instance].skin_index;
	UIImageView* background = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
	background.image = [MCUserDefaults instance].skin_bgViewImg;
	background.autoresizingMask = nMCViewAutoresizingMask | UIViewAutoresizingFlexibleHeight;
	background.tag = -5234;
	[self.view addSubview:background];
	[self.view sendSubviewToBack:background];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)pushViewController: (UIViewController*)controller
    animatedWithTransition: (UIViewAnimationTransition)transition {
	[self pushViewController:controller animated:NO];
	
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.7];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
	[UIView setAnimationTransition:transition forView:self.view cache:YES];
	[UIView commitAnimations];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (UIViewController*)popViewControllerAnimatedWithTransition:(UIViewAnimationTransition)transition {
	UIViewController* poppedController = [self popViewControllerAnimated:NO];
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.7];
	[UIView setAnimationDelegate:self];
	//[UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
	[UIView setAnimationTransition:transition forView:self.view cache:NO];
	[UIView commitAnimations];
	
	return poppedController;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
	if (skin_index != [MCUserDefaults instance].skin_index) {
		self.bgImage = [MCUserDefaults instance].skin_bgViewImg;
		skin_index = [MCUserDefaults instance].skin_index;
	}
	//for (UIViewController* controller in self.viewControllers) {
	//	controller.view.alpha = 0.0;
	//}
	MCLog(@"MCNavigationController push %@", [viewController description]);
	if ([self.visibleViewController isKindOfClass:[LoginCheckController class]] ||
		self.visibleViewController.view.tag == -10000) {
		[self popViewControllerAnimated:NO];
	}
	BOOL queryNeedPop = TRUE;
	if ([viewController isKindOfClass:[MCPopupViewController class]]
		||[viewController isKindOfClass:[SelectFieldViewController class]]
		||[viewController isKindOfClass:[LookupController class]]
		||[viewController isKindOfClass:[TreeLookupController class]]
		||[viewController isKindOfClass:[AListLookupController class]]
		||[viewController isKindOfClass:[FileListViewController class]]
		||[viewController isKindOfClass:[CustomAListLookupController class]]
		||[viewController isKindOfClass:[CustomLookupController class]]
		||[viewController isKindOfClass:[TextFieldViewController class]]) {// AListController huotianjun增加
		queryNeedPop = FALSE;
	}
	int nControllerCount = [self.viewControllers count];
	for (int i = nControllerCount-1; i >= 0; i--) {
		UIViewController* controller = [self.viewControllers objectAtIndex:i];
		if ([controller isKindOfClass:[QueryController class]] && queryNeedPop) {
			//[super popViewControllerAnimated:NO]; //huotianjun 注释掉
			continue;
		}
	}
	
	[super pushViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
	int nCount = [self.viewControllers count];
	UIViewController* controller = nil;
	if (nCount >= 2) {
		controller = [self.viewControllers objectAtIndex:nCount-2];
	} else if (nCount == 1) {
		controller = [self.viewControllers objectAtIndex:nCount-1];
	}
	//controller.view.alpha = 1.0;
	
	if ([controller isKindOfClass:[MenuController class]]) {
		[(MenuController*)controller refreshData];
	}
    
    /*if ([self isKindOfClass:[MCTabBarNavigationController class]]) {//huotianjun 增加，如果是在mctabbarnavigation中push的viewcontroller，弹出后刷新mctabbarnavigation
		//[(MCTabBarNavigationController *)self performSelector:@selector(onRequestMenu) withObject:nil afterDelay:.5];
	}*/
    
	
	MClientAppDelegate *appDelegate = (MClientAppDelegate*)[[UIApplication sharedApplication] delegate];
	[appDelegate stopActivityIndicatorView];
	
	return [super popViewControllerAnimated:animated];
}

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//    return [self.visibleViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
//}

- (UIImage*)bgImage { return nil;}
- (void)setBgImage:(UIImage*)bgImage {
	UIImageView* background = (UIImageView*)[self.view viewWithTag:-5234];
	background.image = bgImage;
}

@end
